package com.yiguantong.police.ui.activity;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.widget.Toast;


import com.yiguantong.police.ui.dialog.LoadingDialog;
import com.yiguantong.police.ui.utils.ActivityStackManager;

import java.lang.ref.WeakReference;

import androidx.appcompat.app.AppCompatActivity;


/**
 * ComBaseActivity
 * UI提示框统一管理、Toolbar统一管理
 */
public class ComBaseActivity extends AppCompatActivity {
    Toast toast;
    private BaseHandler baseHandler = new BaseHandler(this);
    //加载等待框
    private LoadingDialog loading;
    public final int baseHandlerMsgWhatShowDialog = 0x01;
    public final int baseHandlerMsgWhatDismissDialog = 0x02;
    //解决应用双开弹框不消失问题，通过window焦点判断
    public final int baseHandlerMsgWhatDismissDialogJudgeAppBipartition = 0x04;


    private class BaseHandler extends Handler {
        private WeakReference<ComBaseActivity> baseActivity;

        public BaseHandler(ComBaseActivity activity) {
            baseActivity = new WeakReference<>(activity);
        }

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            ComBaseActivity activity = baseActivity.get();
            if (activity == null)
                return;
            switch (msg.what) {
                case baseHandlerMsgWhatShowDialog: {
                    if (loading == null) {
                        loading = new LoadingDialog(ComBaseActivity.this);
                    }
                    if (!loading.isShowing()) {
                        loading.show();
                    }
                }
                break;
                case baseHandlerMsgWhatDismissDialog: {
                    if (loading != null && loading.isShowing()) {
                        loading.dismiss();
                    }
                }
                break;
                case baseHandlerMsgWhatDismissDialogJudgeAppBipartition:
                    if (!ComBaseActivity.this.hasWindowFocus()) {
                        dismissNetWaitingDialog(true);
                    }
                    break;
                default:
                    break;
            }
        }
    }


    /**
     * 提示加载等待对话框
     *
     * @param sync true:UI线程
     *             false:非UI线程
     */
    public void showNetWaitingDialog(boolean sync) {
        if (sync) {
            if (loading == null) {
                loading = new LoadingDialog(ComBaseActivity.this);
            }
            if (!loading.isShowing()) {
                loading.show();
            }
        } else {
            baseHandler.sendMessage(baseHandler.obtainMessage(baseHandlerMsgWhatShowDialog));
        }
    }

    /**
     * 监听返回键,并且设置文本显示
     *
     * @param sync                 是否是主线程
     * @param isNoKeyBoardListener 是否监听键盘
     * @param text                 加载文本
     */
    public void showWaitingDialogListener(boolean sync, boolean isNoKeyBoardListener, String text) {
        if (sync) {
            if (loading == null) {
                loading = new LoadingDialog(ComBaseActivity.this);
            }
            if (!loading.isShowing()) {
                loading.show();
                loading.setmText(text);
                if (isNoKeyBoardListener) {
                    loading.setOnKeyListener(new DialogInterface.OnKeyListener() {
                        @Override
                        public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
                            if (keyCode == KeyEvent.KEYCODE_BACK) {
                                finish();
                                return false;
                            } else {
                                return false; //默认返回 false
                            }
                        }
                    });
                }
            }
        } else {
            baseHandler.sendMessage(baseHandler.obtainMessage(baseHandlerMsgWhatShowDialog));
        }
    }

    /**
     * 隐藏等待对话框(非UI线程不可调用)
     *
     * @param sync true:UI线程
     *             false:非UI线程
     */
    public void dismissNetWaitingDialog(boolean sync) {
        if (sync) {
            if (loading != null && loading.isShowing()) {
                loading.dismiss();
            }
        } else {
            baseHandler.sendMessage(baseHandler.obtainMessage(baseHandlerMsgWhatDismissDialog));
        }
    }


    /**
     * 应用双开时，设置加载框自动消失
     */
    public void dismissNetWaitingDialogBipartition() {
        baseHandler.sendMessageDelayed(baseHandler.obtainMessage(baseHandlerMsgWhatDismissDialogJudgeAppBipartition), 500);
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActivityStackManager.getAppManager().addActivity(this);
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
    }

    //toast消息
    public void toast(final String toastString, boolean mainThread) {
        if (mainThread) {
            toast(toastString);
        } else {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    toast(toastString);
                }
            });
        }
    }

    public void toast(String toastString) {
        if (toast == null) {
            toast = Toast.makeText(ComBaseActivity.this, toastString, Toast.LENGTH_SHORT);
        } else {
            toast.setText(toastString);
        }
        toast.show();
    }
}
